" vimtip2wik.vim
"   Author: Charles E. Campbell, Jr.
"   Date:   Feb 21, 2007
"   Version: 1a	NOT RELEASED

%g/DOCTYPE/d

" get title
1
let tline  = search("<title>Tip #")
let title  = substitute(getline(tline),'^.*<title>Tip #\d\+ - \(.*\)</title>.*$','\1','')
let tipnum = substitute(getline(tline),'^.*<title>Tip #\(\d\+\)\s.*','\1','')

" get created
1
let cline   = search(">created:<") + 2
let created = substitute(getline(cline),'^.*<td>\(.*\)<\td>.*$','\1','')
let created = substitute(created,'</\=td>','','g')
let created = substitute(created,'^\s\+','','')

" get author
1
let aline  = search(">author:<") + 2
let author = substitute(getline(aline),'^.*<td>\(.*\)<\td>.*$','\1','')
let author = substitute(author,'</\=td>','','g')
let author = substitute(author,'^\s\+','','')

" get vim version
1
let vline  = search(">as of Vim:<") + 2
let vimver = substitute(getline(vline),'^.*<td>\(.*\)<\td>.*$','\1','')

" get rating and viewed by
1
let rline    = search("nbsp;Rating <b>")
let rating   = substitute(getline(rline),'^.*Rating <b>\(.\{-}\)</b>.*$','\1','')
let viewedby = substitute(getline(rline),'^.*Viewed by \(\d\+\).\{-}$','\1','')

" get tip contents
1
let cline = search("^<p><code>")
let tip   = substitute(getline(cline),'<p><code>\(.*\)<br></code></p>$','\1','')

" get notes
let inote= 0
1
let nline= search("<h1>Additional Notes</h1>")
while nline > 0
 let inote= inote + 1
 let nline= search('<td class="lightbg">','W')
 if nline == -1
  break
 endif
 let nline              = nline + 1
 let note{inote}_author = substitute(getline(nline),'^\s\+\(\S.*\),$','\1','')
 let nline              = nline + 1
 let note{inote}_date   = substitute(getline(nline),'^.*<font.\{-}>\s\+\(.*\)\s\+</font>$','\1','')
 let nline= search('^\s*<td><tt>','W')
 if nline == -1
  break
 endif
 let note{inote}_comment= substitute(getline(nline),'^\s*<td><tt>\(.*\)$','\1','')
" call Decho("note#".inote.": author<".note{inote}_author.">")
endwhile
let inote= inote - 1

" OK, re-generate tip in wiki format
%d
call setline(1,'= Tip #'.tipnum.": ".title." =")
call setline(2,"||_Author_     || ".author."||")
call setline(3,"||_Created On_ ||".created."||")
call setline(4,"||_Rating_     ||".rating."||")
call setline(5,"||_Viewed By_  ||".viewedby."||")
call setline(6,"")
let @a=tip
$put a
$put =''
$put =''
let lastline= line("$")+1
call setline(lastline,'== Additional Notes ==')

let jnote= 1
while jnote <= inote
 $put =''
 let lastline= line("$")+1
 call setline(lastline,'--- --- --- --- ---')
 let lastline= line("$")+1
 call setline(lastline,"_Note Author:_".note{jnote}_author." _Date_:".note{jnote}_date)
 let @a= note{jnote}_comment
 $put a
 let jnote= jnote + 1
endwhile

" cleanup
silent 1,$s/&nbsp;/ /ge
silent 1,$s/<br>/\r/ge
silent 1,$s/&lt;/</ge
silent 1,$s/&gt;/>/ge
silent 1,$s/&quot;/"/ge
silent 1,$s/<a href="\(http:.\{-\}\)">.\{-}<\/a>/\1/e
silent 1,$s@<a href=.*>vimscript#\(\d\+\)</a>@http://vim.sf.net/scripts/script.php?script_id=\1@ge
silent 1,$s@<a href=.*>vimtip#\(\d\+\)</a>@[vimtip_\1]@ge
%s/\r//ge
wq!

" vim: ts=4 fdm=marker
